package com.example.carbon.server.info;

import com.alibaba.fastjson2.JSON;
import com.example.carbon.dao.MaterialInfoDao;
import com.example.carbon.dao.MaterialProductDao;
import com.example.carbon.dao.ProductModelDao;
import com.example.carbon.pojo.dto.ProduceModelDtoInsert;
import com.example.carbon.server.MaterialInfoServerInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MaterialInfoServer implements MaterialInfoServerInfo{
    @Resource
    MaterialInfoDao materialInfoDao;

    @Resource
    ProductModelDao productModelDao;

    @Resource
    MaterialProductDao materialProductDao;


    /**
     * 根据类别返回物料档案数据
     * @param classification
     * @return
     */
    @Override
    public String selectAllInfo(String classification) {
        try {
            if (classification.equals("")||classification.equals(null)){
                return JSON.toJSONString(materialInfoDao.selectAllInfoBy());
            }
            return JSON.toJSONString(materialInfoDao.selectAllInfoByClassification(classification));
        }catch (Exception e){
            log.info(e.getMessage());
            return null;
        }
    }

    @Override
    public boolean delInfoById(int id) {
        try {
            materialProductDao.deleteRelInfoByModelId(String.valueOf(id));
            return materialInfoDao.delInfoById(id);
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean InsertNewInfo(ProduceModelDtoInsert produceModelDtoInsert) {
        try {
            log.info(produceModelDtoInsert.toString());
            return materialInfoDao.insertNewInfo(produceModelDtoInsert);
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean updateNewInfo(ProduceModelDtoInsert produceModelDtoInsert, int id) {

        try {
            return productModelDao.updateProductModel(produceModelDtoInsert,id);
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }
}
